package com.bhkj.pdjhforotherapp.common.work;

import android.content.Context;

import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.blankj.utilcode.util.LogUtils;
import com.example.reader.sdk.RootCommand;

import java.util.Calendar;

/**
 * 每日执行定时任务
 * 7点-8点执行清除日志业务
 */
public class ClearCacheWork extends Worker {

    public ClearCacheWork(Context context, WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @Override
    public Result doWork() {
        if (compareCurrentHours(8)) {
            clearCache();
        }
        return Result.success();
    }

    private boolean compareCurrentHours(int hours) {
        int currentHours = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        LogUtils.d("时间:" + currentHours);
        return currentHours == hours;
    }

    private void clearCache() {
        RootCommand rootCommand = new RootCommand();
        rootCommand.executeCommands("chmod 777 /data/log");
        rootCommand.executeCommands("rm /data/log/logs.txt");
        rootCommand.executeCommands("rm /data/log/logs_old.txt");
    }


}
